import { useTitle } from "@vueuse/core";
import NProgress from "nprogress";
import { createRouter, createWebHistory } from "vue-router";
import { defaultRoute } from "./modules/defaultRoute";

const router = createRouter({
	history: createWebHistory(import.meta.env.VITE_PUBLIC_PATH),
	routes: defaultRoute,
});

/**
 * 配置 NProgress
 */
NProgress.configure({
	// 动画方式
	easing: "ease",
	// 递增进度条的速度
	speed: 500,
	// 是否显示加载ico
	showSpinner: true,
	// 自动递增间隔
	trickleSpeed: 200,
	// 初始化时的最小百分比
	minimum: 0.3,
});

/**
 * 路由加载前
 */
router.beforeEach(async (to, from, next) => {
	// 开启进度条
	NProgress.start();

	// 刷新页面标题
	const title = useTitle();
	if (to.meta.title) {
		title.value = `${to.meta.title}`;
	}

	next();
});

/**
 * 路由加载后
 */
router.afterEach(() => {
	NProgress.done();
});

export default router;
